<?php

declare(strict_types=1);
/**
 * This file is part of micro-word.
 *
 * @link     https://www.cppo.cn/
 * @contact  1300188034@qq.com
 * @license  https://gitee.com/cppo/micro_word/blob/master/README.md
 */

namespace App\Helpers;

use stdClass;

final class Output
{
    /**
     * 请求失败.
     *
     * @param int $code 返回码
     * @param string $message 返回消息
     * @param array $data 返回值
     */
    public function error(int $code = 50400, string $message = '', array $data = []): array
    {
        $code = empty($code) || ! isset(Code::MSG[$code]) ? Code::ERROR : $code;
        $message = empty($message) ? Code::MSG[$code] : $message;
        return $this->output([
            'code' => $code,
            'message' => $message,
            'data' => empty($data) ? (new stdClass()) : $data,
        ]);
    }

    /**
     * 请求成功
     *
     * @param array $data 返回值
     * @param string $message 返回消息
     */
    public function success(array $data = [], string $message = ''): array
    {
        return $this->output([
            'code' => Code::SUCCESS,
            'message' => $message,
            'data' => empty($data) ? (new stdClass()) : $data,
        ]);
    }

    /**
     * 返回请求
     *
     * @param array $data 返回内容
     */
    private function output(array $data = []): array
    {
        return $data;
    }
}
